package com.example.songyingjie

import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class HistoryActivity : AppCompatActivity() {
    private lateinit var historyListView: ListView


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_history)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        historyListView = findViewById(R.id.historyListView)


        loadHistory()
    }

    private fun loadHistory() {
        val sharedPref = getSharedPreferences("CalculatorPrefs", MODE_PRIVATE)
        val historySet = sharedPref.getStringSet("history", null) ?: return

        val historyArray = historySet.toList()
        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, historyArray)
        historyListView.adapter = adapter
    }
}